<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<style type="text/css">
    #selectOrderDetail .layui-form-select {
        /*position: fixed !important;*/
    }
</style>
<script type="text/html" id="doShipment_template">
    <div>待发货 <span id="totalNum">{{ d.waitShipment }}</span>，已选 <span id="selectNum"></span></div>
    <table class="layui-table layui-form" lay-skin="line">
        <colgroup>
            <col width="50px">
            <col>
            <col width="100px">
            <col>
            <col width="100px">
        </colgroup>
        <thead>
        <tr>
            <th></th>
            <th>商品</th>
            <th>数量</th>
            <th>物流|单号</th>
            <th>状态</th>
        </tr>
        </thead>
        <tbody>
        {{# layui.each(d.outputParents, function(index, outParent){ }}
            <tr>
                <td colspan="5">
                    {{ outParent.distributorName }}
                </td>
            </tr>
            {{# layui.each(outParent.childrens, function(index, od){ }}
            <tr data-id="{{ od.id }}" class="od-child-item">
                <td style="vertical-align: top">
                    <div>
                        {{# if(od.status == 4){ }}
                        <input type="checkbox" name="id" value="{{ od.id }}" lay-filter="chk" lay-skin="primary" disabled>
                        {{# } else { }}
                        <input type="checkbox" name="id" value="{{ od.id }}" lay-filter="chk" lay-skin="primary">
                        {{# } }}
                    </div>
                </td>
                <td>
                    <div>
                        <div class="mb5 ellipsis-1" title="{{ od.name }}">{{ od.name }}</div>
                        {{# if(util.isValid(od.specValues)){ }}
                        <div>{{ od.specValues }}</div>
                        {{# } }}
                    </div>
                </td>
                <td>
                    <div>{{ od.quantity }}</div>
                </td>
                <td class="courier-info">
                    <div class="courier-name">{{ od.courierName || '' }}</div>
                    <div class="courier-number">{{ od.courierNumber || '' }}</div>
                </td>
                <td class="status-info red">
                    {{# if(od.status == 4){ }}
                    已发货
                    {{# } }}
                </td>
            </tr>
            {{# }); }}
        {{# }); }}
        </tbody>
    </table>
    <div class="mb10 mt10">收货地址：{{ d.odPartner.addressFull }}，{{ d.odPartner.addressName }}，{{ d.odPartner.addressPhone
        }}
    </div>
    <form class="layui-form" action="${ctx}/logistics/logistics/doShipment.do" id="shipmentForm">
        <input type="hidden" name="goodsTargetIds"/>
        <input type="hidden" name="courierName"/>
        <input type="hidden" name="type" value="1">
        <input type="hidden" name="targetId" value="{{ d.odPartner.orderId }}"/>
        <div class="layui-form-item" style="display: inline-block">
            <label class="layui-form-label">物流公司：</label>
            <div class="layui-input-inline">
                <select name="courierCode" lay-search>
                    <option value="">请选择一个物流公司</option>
                    {{# layui.each(d.couriers, function(index, express){ }}
                    <option value="{{ express.code }}">{{ express.name }}</option>
                    {{# }); }}
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="display: inline-block">
            <label class="layui-form-label">快递单号：</label>
            <div class="layui-input-inline">
                <input class="layui-input" name="courierNumber" autocomplete="off"/>
            </div>
        </div>
    </form>
</script>
<script type="text/javascript">
    // 发货
    function doShopment(parentId, tr) {
        var $content = null;
        if (tr != null) {
            $content = $(tr).closest(".layui-table");
        } else {
            $content = $(".layui-table");
        }
        var hasApplyRefund = $content.find(".hasApplyRefund").length;
        if (hasApplyRefund > 0) {
            util.layerMsgError("订单中的部分商品，买家已提交了退款申请。你需要先跟买家协商，买家撤销退款申请后，才能进行发货操作。");
            return false;
        }
        txz.ajaxRequest({
            method: 'get',
            url: '${ctx}/mall/order/selectOrderDetail.do',
            saveCache: true,
            cacheObj: null,
            params: {
                orderId: parentId
            },
            callBack: function (res) {
                if (res.success) {
                    var content = $("#selectOrderDetail");
                    $(content).html("");
                    var getTpl = $("#doShipment_template").html();
                    laytpl(getTpl).render(res.data, function (html) {
                        content.append(html);

                        form.render('checkbox');
                        form.render('select');

                        function yesCb(index) {
                            var length = $(content).find("[name=id]:checked").length;
                            if (length == 0) {
                                util.layerMsgError("请先选择需要发货的商品");
                                return false;
                            }
                            var _result = new Array();
                            $(content).find("[name=id]:checked").each(function (index, ele) {
                                var $tr = $(ele).closest("tr");
                                _result.push($tr.data("id"));
                            });
                            $(content).find("[name=goodsTargetIds]").val(_result.join(","));

                            var courierCode = $(content).find("[name=courierCode]").find(":selected").val();
                            if (!util.isValid(courierCode)) {
                                util.layerMsgError("请选择物流公司");
                                return false;
                            }
                            var courierName = $(content).find("[name=courierCode]").find(":selected").text();
                            $(content).find("[name=courierName]").val(courierName);

                            var courierNum = $(content).find("[name=courierNumber]").val().trim();
                            if (!util.isValid(courierNum)) {
                                util.layerMsgError("请填写快递单号");
                                return false;
                            }

                            txz.ajaxRequest({
                                method: 'post',
                                url: $("#shipmentForm").attr('action'),
                                saveCache: null,
                                cacheObj: null,
                                params: $("#shipmentForm").serialize(),
                                callBack: function (res) {
                                    if (res.success) {
                                        var courierName = $(content).find("[name=courierName]").val();
                                        var courierNumber = $(content).find("[name=courierNumber]").val();
                                        for (var i = 0; i < _result.length; i++) {
                                            var $tr = $(content).find("tbody").find("[data-id='" + _result[i] + "']");
                                            $tr.find("input[type=checkbox]").prop("checked", false).prop("disabled", true);
                                            $tr.find(".courier-info .courier-name").text(courierName);
                                            $tr.find(".courier-info .courier-number").text(courierNumber);
                                            $tr.find(".status-info").text("已发货");
                                        }
                                        form.render('checkbox');
                                        var totalNum = $("#totalNum").text();
                                        var selectNum = $("#selectNum").text();
                                        if (totalNum == selectNum) {
                                            location.reload();
                                        }
                                        $("#totalNum").text(totalNum - _result.length);
                                        $("#selectNum").text("0");
                                    } else {
                                        util.layerMsgError("发货异常");
                                    }
                                }
                            });
                        }

                        openDialog('商品发货', '#selectOrderDetail', '800px', '500px', yesCb);
                    });
                } else {
                    util.layerMsgError("加载异常");
                }
            }
        });
    }
</script>